<?php

namespace Web\MainSiteBundle\Controller;

use Model\CourseBundle\Model\CourseModel;
use Model\CourseCollectBundle\Model\CourseCollectModel;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;

/**
 * @route("CourseCollect")
 */
class CourseCollectController extends WebBaseController
{
    /**
     * @route("/add",name="WebCourseCollectAdd")
     */
    public function WebCourseCollectAdd()
    {
        if (!$whom = $this->getParamRequired("id")) {$this->showMessagePage("数据丢失，请刷新后重试");}
        $user = $this->needToBeUser();
        $userId = $user->getId();
        $CourseCollectModel = new CourseCollectModel($this->getDoctrine());
        $courseModel = new CourseModel($this->getDoctrine());
        $course = $courseModel->getOneById($whom,$userId);
        $uniqueId = $course["uniqueId"];
        if ($CourseCollectModel->chkIfCollect($userId,$whom)) {
            return $this->js_out("已收藏",$this->generateUrl("WebCourseIndex",array("uniqueId"=>$uniqueId)));
        } else {
            $params = array(
                "who"   => $userId,
                "whom"  => $whom,
            );
            $CourseCollectModel->commonAdd($params);
            return $this->js_out("已收藏",$this->generateUrl("WebCourseIndex",array("uniqueId"=>$uniqueId)));
        }
    }

    /**
     * @route("/del",name="WebCourseCollectDel")
     */
    public function WebCourseCollectDel()
    {
        $user = $this->needToBeUser();
        $userId = $user->getId();
        if (!$whom = $this->getParamRequired("id")) {$this->showMessagePage("数据丢失，请刷新后重试");}
        $CourseCollectModel = new CourseCollectModel($this->getDoctrine());
        $CourseCollectModel->delete($userId,$whom);
        $courseModel = new CourseModel($this->getDoctrine());
        $course = $courseModel->getOneById($whom,$userId);
        $uniqueId = $course["uniqueId"];
        return $this->js_out("已取消收藏",$this->generateUrl("WebCourseIndex",array("uniqueId"=>$uniqueId)));
    }

    /**
     * @route("/my",name="WebCourseCollectMy")
     */
    public function WebCourseCollectMy()
    {
        $user = $this->needToBeUser();
        $CourseCollectModel = new CourseCollectModel($this->getDoctrine());
        $param = array();
        $param["conditions"]["CourseCollect"]["who"]["eq"]=$user->getId();
        $lists = $CourseCollectModel->getAdminLists($param)["data"];
        $courseModel = new CourseModel($this->getDoctrine());

        foreach ($lists as &$list) {
            $statistic = $courseModel->getStatistic($list["Course_uniqueId"]);
            $list["statistic"] = $statistic;
        }
        $data["collects"] = $lists;

        return $this->render("WebMainSiteBundle:User:user_collect.html.twig",$data);
    }
}
